<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 登陆，注册，注销
 *
 *
 */
class Login extends CI_Controller
{
	function __construct()
    {
        parent::__construct();
        $this->load->model('customer_model');
    }
   
    
    // --------------------------------------------------------------------

    /**
	 * 登陆，注册 界面
	 *
	 *
	 */	
    function index()
    { 
	    $data = array();
		$data['css'][0] = "<link type='text/css' rel='stylesheet' href='".base_url()."css/login.css'>";
		$data['css'][1] = "<link type='text/css' rel='stylesheet' href='".base_url()."css/jq_login.css'>";
	    $data['title'] = '用户登录';

		$data['error_msg'] = $this->uri->segment(3, 0);
	    $this->load->view('login',$data);
    }
    
    
	// --------------------------------------------------------------------

    /**
	 * 登陆生成验证码
	 *
	 *
	 */	
	function yzm_img()
	{
		$this->load->helper('captcha1');
		code();
	}
    
	// --------------------------------------------------------------------

    /**
	 * 检查登陆验证码是否准确
	 *
	 *
	 */	
	function _check_yzm()
	{
		session_start();
        $Verifier = $this->input->post('tbxLoginVerifier');
        if (!empty($_SESSION['verify1']) && $_SESSION['verify1'] == $Verifier){
			//
		}else{
			redirect('login/index/verifier1');
		}
		return;
	}
    

    
	// --------------------------------------------------------------------
	
	function on_user_login($name, $password)
	{
		$from_iPhone = $this->input->post("fromiPhone") == "true" ? true : false;
		$this->customer_model->name = $name;
		$this->customer_model->password = $password;
		$_customer = $this->customer_model->check_customer();
		if ($_customer){
			$customer = array(
					'user_name'  => $_customer['nickname'],
					'user_id'  => $_customer['uid'],
					'user_in' => TRUE,
					'user_last_login' => $_customer['lastloginat']
			);
			$this->session->set_userdata($customer);
		
			$this->customer_model->update_user_last_login($_customer['uid']);
			
			if($from_iPhone)
			{
				$seq = $this->input->post("seq");
				echo $seq.'|';
				echo "success";
				return;
			}
		
			redirect('main_function_entry');
		}else{
			if($from_iPhone)
			{
				$seq = $this->input->post("seq");
				echo $seq.'|';
				echo "failure";
				return;
			}
			
			redirect('login');
		}
	}
	
	function on_admin_login($name, $password)
	{
		$this->customer_model->name = $name;
		$this->customer_model->password = $password;
		$_admin = $this->customer_model->check_admin();
		if($_admin){
			$admin = array(
					"admin_name" => $_admin["account"],
					"admin_id" => $_admin["aid"],
					"admin_in" => TRUE,
					"admin_last_login" => $_admin["lastloginat"]
					);
			$this->session->set_userdata($admin);
			$this->customer_model->update_admin_last_login($_admin["aid"]);
			redirect('admin');
		}else
		{
			redirect("login");
		}
	}

    /**
	 * 登陆
	 *
	 *
	 */	
	function check_customer()
	{
		$from_iPhone = $this->input->post("fromiPhone") == "true" ? true : false;
		
		if(!$from_iPhone){
			$this->_check_yzm();
		}
		
		$name = $this->input->post('tbxLoginNickname');
		$password = $this->input->post('tbxLoginPassword');
		$is_admin = strtolower($this->input->post('tbxAdmin'));
		
		
		if($is_admin=="on" || $is_admin == "off")
		{
			$this->on_admin_login($name, $password);
		}
		else
		{
			$this->on_user_login($name, $password);
		}

	}
}
